<template>
	<jk-dialog
		v-model="showDialog"
		title="停机类型"
		width="500px"
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false">
			<el-row>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="机台">
						<span class="read-only-2">{{ ruleForm.machineName || '' }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="停机日期">
						<span class="read-only-2">{{ ruleForm.beginTime ? ruleForm.beginTime.split(' ')[0] : '' }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="停机时间">
						<span class="read-only-2">{{ ruleForm.beginTime ? `${ filterTime(ruleForm.beginTime) } ~ ${ filterTime(ruleForm.endTime) }` : '' }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="停机原因" prop="typeId">
						<jk-select v-model="ruleForm.typeId" size="medium" :option-list="shutdownList" class="width-100-percent" placeholder="停机原因"></jk-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../mixins/cal-dialog-height';
    import { systemBusinessDictPage } from '../../../api/basicData/businessDict';
    import { productionMachineShutdownRecordsUpdate } from '../../../api/production/reports';

    export default {
        mixins: [calDialogHeight],
        props: {
            dataItem: {
                type: Object,
                default() {
                    return {};
                }
            },
            dialogState: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                ruleForm: {
                    typeId: ''
                },
                rules: {
                    typeId: [{ required: true, trigger: 'change' }]
                },
                shutdownList: [],
                showDialog: false,
                confirmBtnLoading: false
            };
        },
        methods: {
            filterTime(e) {
                return e ? e.split(' ')[1] : '';
            },
            getListRequest() {
                this.tableLoading = true;
                return systemBusinessDictPage({
                    categoryCode: 'SHUTDOWN_TYPE'
                }).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.shutdownList = res.data.res || [];
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.confirmBtnLoading = true;
                        const params = JSON.parse(JSON.stringify(this.ruleForm));
                        const typeItem = this.shutdownList.find(x => x.id === params.typeId) || {};
                        params.typeCode = typeItem.code;
                        params.typeName = typeItem.name;
                        productionMachineShutdownRecordsUpdate(params).then(res => {
                            this.confirmBtnLoading = false;
                            if (res.data.status === 200) {
                                this.$emit('on-confirm');
                            }
                        });
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.ruleForm = this.dataItem ? JSON.parse(JSON.stringify(this.dataItem)) : {};
                    this.getDependentData();
                }
            }
        }
    };
</script>

